package geek;

import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author sam
 * 2021/10/19 22:34
 * @description
 * @editor
 * @description 堆栈实现队列
 * 队列实现堆栈
 */
public class Lesson09 {

    public static class CustomQueue {
        private Stack<String> input = new Stack<>();
        private Stack<String> output = new Stack<>();

        public void put(String value) {
            input.push(value);
        }

        public String pop() {
            if (output.isEmpty()) {
                for (String s : input) {
                    output.push(s);
                }
            }
            if (!output.isEmpty()) {
                return output.pop();
            }
            return null;
        }

        public boolean isEmpty() {
            return input.isEmpty() && output.isEmpty();
        }
    }

    public static class CustomStack {
        private Queue<String> input = new LinkedBlockingQueue<>();
        private Queue<String> output = new LinkedBlockingQueue<>();
        /*
        1 2 3 4 5
        null

        null
        4 3 2 1

        6 7 8
        4321

        null
        8764321
         */

        public void put(String value) {
            input.add(value);
        }

        public String pop() {
            while (!input.isEmpty()) {
                output.add(input.poll());
            }
            if (!output.isEmpty()) {
                return output.poll();
            }
            return null;
        }

        public boolean isEmpty() {
            return input.isEmpty() && output.isEmpty();
        }
    }
}
